﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PolarImport.DeviceImporter
{
    internal class DeviceConfigurationInfo
    {
        
        public DeviceConnection UploadConnection;
        public DeviceConnection DownloadConnection;
        public int ComPort;
        public int SamplingFrequency;
        public int Amplification;
        public bool WriteBasicValues;
        public bool WriteTimeBasedValues;
        public bool WriteLapData;
        public bool WriteDetailedInfos;
        public string DeviceName;
        public MonitorType PolarDevice;

        public DeviceConfigurationInfo()
        {
            this.ComPort = 1;
        }

        public override string ToString()
        {
            return ("UploadConnection=" + this.UploadConnection.ToString() +
                    ";DownloadConnection=" + this.DownloadConnection.ToString() +
                    ";ComPort=" + this.ComPort.ToString() +
                    ";SamplingFrequency=" + this.SamplingFrequency.ToString() +
                    ";Amplification=" + this.Amplification.ToString() +
                    ";BasicValues=" + this.WriteBasicValues.ToString() +
                    ";TimeBasedValues=" + this.WriteTimeBasedValues.ToString() +
                    ";LapData=" + this.WriteLapData.ToString() +
                    ";Detailed=" + this.WriteDetailedInfos.ToString() +
                    ";Name=" + this.DeviceName +
                    ";Device=" + this.PolarDevice.ToString());
        }

        public static DeviceConfigurationInfo Parse(string configurationInfo)
        {
            DeviceConfigurationInfo info = new DeviceConfigurationInfo();
            if (configurationInfo != null)
            {
                foreach (string configStr in configurationInfo.Split(new char[] { ';'}))
                {
                    string configKey;
                    string configValue;

                    string[] configDetail = configStr.Split(new char[] { '=' });
                    if ((configDetail.Length == 2) && 
                        ((configKey = configDetail[0]) != null) &&
                        ((configValue = configDetail[1]) != null))
                    { 
                        switch (configKey)
                        {
                            case "UploadConnection":
                                {
                                    info.UploadConnection = (DeviceConnection)Enum.Parse(typeof(DeviceConnection), configValue);
                                    break;
                                }
                            case "DownloadConnection":
                                {
                                    info.DownloadConnection = (DeviceConnection)Enum.Parse(typeof(DeviceConnection), configValue);
                                    break;
                                }
                            case "ComPort":
                                {
                                    info.ComPort = int.Parse(configValue);
                                    break;
                                }
                            case "SamplingFrequency":
                                {
                                    info.SamplingFrequency = int.Parse(configValue);
                                    break;
                                }
                            case "Amplification":
                                {
                                    info.Amplification = int.Parse(configValue);
                                    break;
                                }
                            case "BasicValues":
                                {
                                    info.WriteBasicValues = bool.Parse(configValue);
                                    break;
                                }
                            case "TimeBasedValues":
                                {
                                    info.WriteTimeBasedValues = bool.Parse(configValue);
                                    break;
                                }
                            case "LapData":
                                {
                                    info.WriteLapData = bool.Parse(configValue);
                                    break;
                                }
                            case "Detailed":
                                {
                                    info.WriteDetailedInfos = bool.Parse(configValue);
                                    break;
                                }
                            case "Name":
                                {
                                    info.DeviceName = configValue;
                                    break;
                                }
                            case "Device":
                                {
                                    info.PolarDevice = (MonitorType)Enum.Parse(typeof(MonitorType), configValue);
                                    break;
                                }
                        }
                    }
                }
            }
            return info;
        }
    }
}
